package com.caiyi.financial.nirvana.sms.mapper;


import com.caiyi.financial.nirvana.sms.base.mapper.BaseMapper;
import com.caiyi.financial.nirvana.sms.entity.SmsWhiteList;
import com.caiyi.financial.nirvana.sms.querybean.QueryMsgBean;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;
import java.util.Map;

@Mapper
public interface SmsWhiteListMapper extends BaseMapper<SmsWhiteList> {

    @Delete("delete from sms_white_list where phone=#{phone} and account = #{account}")
    int delSmsWhiteList(@Param("phone") String phone, @Param("account") String account);

    @SelectProvider(type = MsgSqlProvider.class, method = "searchSmsWhiteList")
    List<SmsWhiteList> searchSmsWhiteList(QueryMsgBean bean);

    @Select("select count(*) from sms_white_list where phone=#{phone} and account = #{account}")
    int getSmsWhiteList(@Param("phone") String phone, @Param("account") String account);


    class MsgSqlProvider {
        public String searchSmsWhiteList(QueryMsgBean bean) {
            return new SQL() {
                {
                    SELECT("id,account,phone,create_time as createTime,update_time as updateTime");
                    FROM("sms_white_list");
                    if (StringUtils.isNotBlank(bean.getAccount())) {
                        WHERE("account = #{account} ");
                    }
                    if (StringUtils.isNotBlank(bean.getPhone())) {
                        WHERE("phone like concat('%',#{phone},'%')");
                    }
                    if (bean.getStartTime() != null) {
                        WHERE("(create_time >= #{startTime} or create_time is null)");
                    }
                    if (bean.getEndTime() != null) {
                        WHERE("(create_time < date_add(#{endTime}, interval 1 day) or create_time is null)");
                    }
                    ORDER_BY("id desc");
                }
            }.toString();
        }

    }
}
